﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：InventoryManage.ascx.cs
//
//文件功能描述：商品库存管理
// 
//创建标识：2007.11.21 曹骞
//创建描述：
//
//修改标识：2007.11.30 曹骞
//修改描述:添加删除提示
//
 ------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using JXMSTC.SmartBusiness.BFL;
using JXMSTC.SmartBusiness.BFL.IBLL;
using JXMSTC.SmartBusiness.BFL.BLLFactory;
using localhost;

/// <summary>
/// 商品库存管理
/// </summary>
public partial class UserControls_InventoryManage : System.Web.UI.UserControl
{
    
   private  static DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
               //调用绑定商品类别的方法
                GetInventoryList();
                GetInventory();

        }
    }
    /// <summary>
    ///  按照商品类别和商品名搜索商品库存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
         //获取满足条件的商品列表
            GetInventory();
    }
    /// <summary>
    /// 获取异步调用的回调结果
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void InventoryMng_GetInventoryCompleted(object sender, GetInventoryCompletedEventArgs e)
    {
        //绑定数据源
        
        ds = e.Result;
        this.GridviewBind(ds);
      
    }

    /// <summary>
    /// 获取商品类别列表
    /// </summary>
    void GetInventoryList()
    {
        //获取商品小类别列表
        ProductService Service = new ProductService();
        DataSet ds1 = new DataSet();
        ds1 = Service.GetLowCategoryList();
        this.dropCategory.DataSource = ds1.Tables[0];
        //
        //绑定dropCategory的DataTextField和DatavalueField的字段值 
        //
        this.dropCategory.DataTextField = ds1.Tables[0].Columns["Name"].ToString();
        this.dropCategory.DataValueField = ds1.Tables[0].Columns[0].ToString();
        this.dropCategory.DataBind();
        
    }

    /// <summary>
    /// 获取商品列表
    /// </summary>
    void GetInventory()
    {   
        //获取当前选中的商品类别名
        string ProductCategory = this.dropCategory.SelectedValue.ToString();
        string ProductName = this.txtProductName.Text;
        ProductService InventoryMng = new ProductService();

        //获取商品库存信息
        //DataSet ds = new DataSet();
        ds=InventoryMng.GetInventory(ProductCategory, ProductName);
        this.GridviewBind(ds);
 
    }

    /// <summary>
    ///  分页显示 
    /// </summary>
    /// <param name="ds"></param>
    private void GridviewBind(DataSet ds)
    {
        //初始绑定时显示翻页超链
        btnFirst.Visible = true;
        btnPrev.Visible = true;
        btnNext.Visible = true;
        btnLast.Visible = true;

        LblCurrentIndex.Visible = true;
        LblPageCount.Visible = true;
        LblRecordCount.Visible = true;
        LblNoRecord.Visible = false;
           
        try
        {   
            //绑定数据源
            this.gdvInventory.DataSource = ds.Tables[0].DefaultView;
            this.gdvInventory.DataBind();

            //显示分页提示
            LblCurrentIndex.Text = "第 " + (this.gdvInventory.PageIndex + 1).ToString() + " 页";
            LblPageCount.Text = "共 " + gdvInventory.PageCount.ToString() + " 页";
            LblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";

            if (ds.Tables[0].Rows.Count == 0)
            {
               //如果当前记录条数为0 

                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;

                LblCurrentIndex.Visible = false;
                LblPageCount.Visible = false;
                LblRecordCount.Visible = false;

                LblNoRecord.Visible = true;
            }
            else if (gdvInventory.PageCount == 1)
            {
                //否则不显示分页超链
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
            }
        }
        catch (Exception ex)
        {
            Response.Write("数据库错误，错误原因：" + ex.Message);
            Response.End();
        }

    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        //翻页按钮
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            //上一页
            case "prev":
                if (gdvInventory.PageIndex > 0)
                {
                    gdvInventory.PageIndex -= 1;
                }
                break;

            //下一页
            case "next":
                if (gdvInventory.PageIndex < (gdvInventory.PageCount - 1))
                {
                    gdvInventory.PageIndex += 1;
                }
                break;
  
            //最后一页
            case "last":
                gdvInventory.PageIndex = (gdvInventory.PageCount - 1);
                break;

             //首页
            default:
                gdvInventory.PageIndex = 0;
                break;
        }

        this.GridviewBind(ds);
    }
    /// <summary>
    /// 删除记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        //获取当前要删除行的主键 
        string strInventoryID = ((ImageButton)sender).CommandArgument.Trim();
        ProductService Delete = new ProductService();
        bool result = false;
        try
        {
            //删除当前选中行
            result = Delete.DeleteInventory(strInventoryID);

            //删除后刷新gridview
            GetInventory();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}
